#include <stdio.h>
#include <assert.h>
#include <iostream>
typedef int QDataType;

typedef struct QListNode
{
    struct QListNode* next;
    QDataType data;
}QListNode;


typedef struct Queue
{
    QListNode* head;
    QListNode* tail;
}Queue;


void QueueInit(Queue* pq)
{
    assert(pq);
    pq->head = nullptr;
    pq->tail = nullptr;
}

void QueueDestory(Queue* pq)
{
    assert(pq);

    QListNode* cur = pq->head;
    while(cur != nullptr)
    {
        QListNode* next = cur->next;
        free(cur);
        cur = next;
    }
    pq->head = pq->tail = nullptr;
}

void QueuePush(Queue* pq, QDataType x)
{
    assert(pq);

    QListNode* newnode = (QListNode*)malloc(sizeof(QListNode));
    if(newnode == nullptr)
    {
        printf("malloc fail!");
        exit(-1);
    }

    newnode->data = x;
    newnode->next = nullptr;
    if(pq->head == nullptr)
    {
        pq->head = pq->tail = newnode;
    }
    else
    {
        pq->tail->next = newnode;
        pq->tail = newnode;
    }
}

bool QueueEmpty(Queue* pq)
{
    return pq->head == nullptr;
}


void QueuePop(Queue* pq)
{
    assert(pq);
    assert(!QueueEmpty(pq));

    if(pq->head->next == nullptr)
    {
        free(pq->head);
        pq->head = pq->tail = nullptr;
    }
    else
    {
        QListNode* next = pq->head->next;
        free(pq->head);
        pq->head = next;
    }
}


QDataType QueueFront(Queue* pq)
{
    assert(pq);
    assert(!QueueEmpty(pq));
    return pq->head->data;
}


QDataType QueueBack(Queue* pq)
{
    assert(pq);
    assert(!QueueEmpty(pq));
    return pq->tail->data;
}


int QueueSize(Queue* pq)
{
    assert(pq);
    
    QListNode* cur = pq->head;
    int size = 0;
    while(cur != nullptr)
    {
        size++;
        cur = cur->next;
    }
    return size;
}